package com.example.toutiao.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.toutiao.R;
import com.example.toutiao.adapter.ProductAdapter;
import com.example.toutiao.db.DatabaseHelper;
import com.example.toutiao.db.Product;

import java.util.ArrayList;
import java.util.List;
//商城模块
public class MallFragment extends Fragment {

    private RecyclerView recyclerView;
    private List<Product> productList;
    private ProductAdapter adapter;
    private DatabaseHelper dbHelper;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mall, container, false);
        recyclerView = view.findViewById(R.id.recycler_products);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        dbHelper = new DatabaseHelper(getContext());

        // 清空现有商品列表
        productList = new ArrayList<>();

        // 从数据库加载商品
        productList.addAll(dbHelper.getAllProducts());  // 获取所有商品

        for (Product product : productList) {
            Log.d("Product", "ID: " + product.getId() + ", Name: " + product.getName());
        }

        // 创建适配器并设置
        adapter = new ProductAdapter(getContext(), productList);
        recyclerView.setAdapter(adapter);

        return view;
    }
}


